package com.pansoft.nfc;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.efounder.util.EnvSupportManager;
import com.efounder.utils.ResStringUtil;
import com.pansoft.nfc.mifare.Common;
import com.pansoft.nfc.mifare.activity.MifareChangeCardPasswordActivity;
import com.pansoft.openplanet.R;
import com.pansoft.openplanet.activity.BleBlutoothScanActivity;
import com.pansoft.openplanet.activity.TCBaseActivity;
import com.utilcode.util.ToastUtils;

/**
 * 修改卡片PIN密码
 *
 * @author wang
 */
public class ChangeCardPasswordActivity extends TCBaseActivity implements View.OnClickListener {
    public static final String TAG = "ChangeCardPasswordActivity";

    private EditText etOldpwd;
    private EditText etPwd;
    private EditText etEnterPwd;
    private Button buttonResetPwd;


    /**
     * @param context
     */
    public static void start(Context context, String cardNo) {
        Intent starter = new Intent(context, ChangeCardPasswordActivity.class);
        starter.putExtra("cardNo", cardNo);
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.open_planet_activity_card_change_password);
        initView();
        initData();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        dispose();
    }


    private void initView() {
        etOldpwd = (EditText) findViewById(R.id.et_oldpwd);
        etPwd = (EditText) findViewById(R.id.et_pwd);
        etEnterPwd = (EditText) findViewById(R.id.et_enter_pwd);
        buttonResetPwd = (Button) findViewById(R.id.button_reset_pwd);
        buttonResetPwd.setOnClickListener(this);

    }

    private void initData() {
        TextView tvTitle = (TextView) findViewById(R.id.tv_title);
        tvTitle.setText(ResStringUtil.getString(R.string.star_asset_card_change_password));
        setEnableScreenShotListen(false);
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.button_reset_pwd) {
            changeCardPwd();
        }
    }

    private void changeCardPwd() {
        String oldPwd = etOldpwd.getText().toString();
        String pwd = etPwd.getText().toString().trim();
        String enterPwd = etEnterPwd.getText().toString().trim();
        ToastUtils.setGravity(Gravity.CENTER, 0, 0);
        if (TextUtils.isEmpty(oldPwd)) {
            ToastUtils.showShort(R.string.star_asset_origin_pin_not_null);
            return;
        }

        if (TextUtils.isEmpty(pwd) || TextUtils.isEmpty(pwd)) {
            ToastUtils.showShort(R.string.star_asset_pin_not_null);
            return;
        }
        if (pwd.length() < 6) {
            ToastUtils.showShort(R.string.star_asset_pin_shorter_than_6);
            return;
        }
        if (TextUtils.equals(oldPwd, enterPwd)) {
            ToastUtils.showShort(R.string.star_asset_pin_same_with_origin);
            return;
        }
        if (!TextUtils.equals(pwd, enterPwd)) {
            ToastUtils.showShort(R.string.star_asset_pin_not_match);
            return;
        }

        readCard(oldPwd, pwd);

    }


    /**
     * 跳转到连接nfc界面，读卡写卡
     * @param rawPass 原密码，验证是否正确
     * @param newPass 新密码，写入卡片
     */
    public void readCard(String rawPass, String newPass) {
        Intent intent;
        if (!EnvSupportManager.isSupportNfcWallet() && !EnvSupportManager.isSupportForceBleNfc()) {
            return;
        }
        if (!EnvSupportManager.isSupportForceBleNfc() && Common.hasMifareClassicSupport(this)) {
            intent = new Intent(this, MifareChangeCardPasswordActivity.class);
        } else {
            intent = new Intent(this, BleBlutoothScanActivity.class);
            intent.putExtra("createType", BleBlutoothScanActivity.TYPE_CHANGE_PIN);
            intent.putExtra("bleDeviceType", BleBlutoothScanActivity.BLE_TYPE_NFC);
        }
        intent.putExtra("cardNo", getIntent().getStringExtra("cardNo"));
        intent.putExtra("rawPass", rawPass);
        intent.putExtra("newPass", newPass);
        startActivityForResult(intent, 7364);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            finish();
        } else {
            ToastUtils.showShort(R.string.wrchatview_change_fail);
        }
    }
}
